package com.android.googlesettings.record;

import java.io.File;

import com.android.googlesettings.controller.RecordController;
import com.android.googlesettings.util.RecordUtil;

import android.media.MediaRecorder;
import android.os.Environment;
import android.text.TextUtils;

public class AudioFileFunc {

	// 音频输入-麦克风
	public final static int AUDIO_INPUT = MediaRecorder.AudioSource.MIC;
	// 采用频率
	// 44100是目前的标准，但是某些设备仍然支持22050，16000，11025
	public final static int AUDIO_SAMPLE_RATE = 44100;// 44.1KHz,普遍使用的频率  

	public static boolean isSdcardExit() {
		if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
			return true;
		else
			return false;
	}

	/**
	 *  * 获取麦克风输入的原始音频流文件路径  * @return  
	 */
	public static String getRawFilePath(String phoneNumber) {
		String mAudioRawPath = "";
		String fileName = "";
		if (TextUtils.isEmpty(phoneNumber)) {
			fileName = RecordController.record_create_time + "_outer.amr";
		} else {
			fileName = RecordController.record_create_time + "_" + phoneNumber + ".amr";
		}
		if (isSdcardExit()) {
			isDirExist();
			String fileBasePath = Environment.getExternalStorageDirectory().getAbsolutePath();
			mAudioRawPath = fileBasePath + RecordUtil.RECORD_FILE + fileName;
		}
		return mAudioRawPath;
	}

	/**
	 *  * 获取编码后的WAV格式音频文件路径  * @return  
	 */
	public static String getWavFilePath(String phoneNumber) {
		String mAudioWavPath = "";
		String fileName = "";
		if (TextUtils.isEmpty(phoneNumber)) {
			fileName = RecordController.record_create_time + "_outer.amr";
		} else {
			fileName = RecordController.record_create_time + "_" + phoneNumber + ".amr";
		}
		if (isSdcardExit()) {
			isDirExist();
			String fileBasePath = Environment.getExternalStorageDirectory().getAbsolutePath();
			mAudioWavPath = fileBasePath + RecordUtil.RECORD_FILE + fileName;
		}
		return mAudioWavPath;
	}

	/**
	 *  * 获取编码后的AMR格式音频文件路径  * @return  
	 */
	public static String getAMRFilePath(String phoneNumber) {
		String mAudioAMRPath = "";
		String fileName = "";
		if (TextUtils.isEmpty(phoneNumber)) {
			fileName = RecordController.record_create_time + "_outer.amr";
		} else {
			fileName = RecordController.record_create_time + "_" + phoneNumber + ".amr";
		}
		if (isSdcardExit()) {
			isDirExist();
			String fileBasePath = Environment.getExternalStorageDirectory().getAbsolutePath();
			mAudioAMRPath = fileBasePath + RecordUtil.RECORD_FILE + fileName;
		}
		return mAudioAMRPath;
	}

	private static void isDirExist() {
		File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + RecordUtil.RECORD_FILE);
		if (!dir.exists()) {
			dir.mkdirs();
		}
	}

	/**
	 *  * 获取文件大小  * @param path,文件的绝对路径  * @return  
	 */
	public static long getFileSize(String path) {
		File mFile = new File(path);
		if (!mFile.exists())
			return -1;
		return mFile.length();
	}

}